home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / PostScript OSA / DropShell3 / FileIterators.h < prev   
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.2 KB  |  148 lines

  1. /*
  2.     A set of iterators for traversing collections of FSSpecs.
  3.     The basic loop should look like:
  4.         SomeKindOfIter    iter ( vol, dir, options );
  5.         iter.Start ();
  6.         while ( iter.More ()) {
  7.             err = iter.Next ( &spec, &bool );
  8.             }
  9.  
  10.     The options that you can pass to the constructor are: (you can mix them, too)
  11.         kNoOptions -- the default, show all files and folders.
  12.         kJustFiles -- skips folders.
  13.         kJustVisible -- skips invisible files and folders
  14.         (Note: Some files/folders are hidden by the finder, and are not
  15.             actually invisible. These include the "Desktop Folder", the Trash folder,
  16.             and the "VM Storage" file.
  17.         kResolveAliases -- if you get a file that is an alias, then resolve it,
  18.             and return what it points to.
  19.  
  20. Implementation notes:
  21.     If you use kJustFiles or kJustVisible, there will be times that More() will return true,
  22.     and then Next () will return fnfErr. This means that More () saw that there were more files
  23.     in the directory to process, but none of them were files (kJustFiles) or visible (kJustVisible).
  24.     Ignore this error and continue.
  25.     Also, if you specify kResolveAliases, watch for Alias Manager errors when the alias gets resolved.
  26.     
  27. */
  28. #ifndef    _FILEITERATORS_
  29. #define    _FILEITERATORS_
  30.  
  31. #ifndef    __FILES__
  32. #include <Files.h>
  33. #endif
  34.  
  35. enum {
  36.     kNoOptions = 0,
  37.     kJustFiles = 1,
  38.     kJustVisible = 2,
  39.     kResolveAliases = 4
  40.     };
  41.  
  42. //    Abstract base class for a file iterator
  43. class TFileIterator {
  44. protected:
  45.     long    fOptions;
  46.     TFileIterator ( long options = kNoOptions );
  47.     
  48. public:
  49.  
  50. //    Copy from another
  51.     TFileIterator ( TFileIterator &rhs );
  52.  
  53.     virtual ~TFileIterator ();
  54.     
  55. //    Assignment
  56.     TFileIterator& operator= ( TFileIterator& rhs );
  57.  
  58.     virtual void    Start ( void )                = 0;
  59.     virtual Boolean    More  ( void )                = 0;
  60.     virtual OSErr    Next  ( FSSpec *theSpec, Boolean *isFolder = NULL )    = 0;
  61.     };
  62.  
  63.  
  64.  
  65. //    Walk the files in a single folder
  66. class TDirectoryIterator : public TFileIterator {
  67. private:
  68.     TDirectoryIterator ();    //    Nobody calls this!
  69.  
  70. protected:
  71.     short            fVRefNum;        // The volume
  72.     long            fParID;            //    and directory that we are handling
  73.     unsigned long    fModDate;        // last modification date of this folder
  74.     long            fNextIdx;        //     which item in the directory are we looking at
  75.     long            fNumItems;        //    how many items in the directory
  76.  
  77.     CInfoPBRec        fPb;    //    used to get the info.
  78.  
  79.     void Synchronize ( void );    //    detects if the directory has changed, and
  80.                                 //    adjusts the internal counters appropriately.
  81.  
  82. //    Get the next item in the directory
  83.     OSErr    GetNextItem ( FSSpec *theSpec, Boolean *isFolder );
  84.  
  85. public:
  86.  
  87. //    Start from an FSSpec
  88.     TDirectoryIterator ( FSSpec *startPoint, long options = kNoOptions );
  89.  
  90. //    If this is a working directory, resolve it and start there
  91. //    If it is a volume & dirID, start there
  92. //    If it is a volume only, start at the root
  93.     TDirectoryIterator ( short vRefNum, long dirID = fsRtDirID, long options = kNoOptions );
  94.  
  95. //    Copy from another
  96.     TDirectoryIterator ( TFileIterator &rhs );
  97.  
  98.     virtual ~TDirectoryIterator ();
  99.     
  100. //    Assignment
  101.     TDirectoryIterator& operator= ( TDirectoryIterator& rhs );
  102.  
  103.     virtual void    Start ( void );
  104.     virtual Boolean    More  ( void );
  105.     virtual OSErr    Next  ( FSSpec *theSpec, Boolean *isFolder = NULL );
  106.     };
  107.  
  108.  
  109. //    Walk and recurse
  110. //    Note that using kResolveAliases can lead to 
  111. //    a search that never finishes.
  112. //(    _Don't_ try to walk the "Recent Servers" folder, for example. :-)
  113. class TFileTreeIterator : public TDirectoryIterator {
  114. protected:
  115.     TDirectoryIterator    *fChild;
  116.  
  117.     TFileTreeIterator ();    //    Nobody calls this!
  118. public:
  119. //    Start from an FSSpec
  120.     TFileTreeIterator ( FSSpec *startPoint, long options = kNoOptions );
  121.  
  122. //    If this is a working directory, resolve it and start there
  123. //    If it is a volume & dirID, start there
  124. //    If it is a volume only, start at the root
  125.     TFileTreeIterator ( short vRefNum, long dirID = fsRtDirID, long options = kNoOptions );
  126.  
  127. //    Copy from another
  128.     TFileTreeIterator ( TFileTreeIterator &rhs );
  129.  
  130.     virtual ~TFileTreeIterator ();
  131.     
  132. //    Assignment
  133.     TFileTreeIterator& operator= ( TFileTreeIterator& rhs );
  134.  
  135.     virtual void    Start ( void );
  136.     virtual Boolean    More  ( void );
  137.     virtual OSErr    Next  ( FSSpec *theSpec, Boolean *isFolder = NULL );
  138.     };
  139.  
  140.  
  141. //    Start at a file/folder and proceed up to the root
  142. class TFilePathIterator : public TFileIterator {
  143.     };
  144.  
  145.  
  146. //    Walk a collection of files ???
  147. #endif
  148.